home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Graphics⁄Sound / crit / voice.c < prev   
Encoding:
C/C++ Source or Header  |  1987-05-23  |  3.8 KB  |  163 lines  |  [TEXT/????]

  1. /*
  2.  * voice.c - the talking bore who knows what's wrong with anything.
  3.  *
  4.  * Each sentence is of the following form:
  5.  *  The <truthadj> <probnoun> is the <badadj> <badnoun> .
  6.  */
  7.  
  8. #include <memory.h>
  9. #include <quickdraw.h>
  10. #include <window.h>
  11. #include <osutil.h>
  12.  
  13. #include "critic.h"
  14. #include "speech.h"
  15.  
  16. extern char *truthadj[];    /* a truth adjective */
  17. extern char *probnoun[];    /* a problem noun */
  18. extern char *badadj[];        /* derogatory adjectives */
  19. extern char *badnoun[];        /* derogatory nouns */
  20.  
  21. int tasize;    /* number of elements in truthadj[]    */
  22. int pnsize;    /* number of elements in probnoun[]    */
  23. int basize;    /* number of elements in badadj[]    */
  24. int bnsize;    /* number of elements in badnoun[]    */
  25.  
  26. SpeechHandle theSpeech;    /* connection to the speech driver (0 == no driver)    */
  27. #define STATLEN    400    /* max # of chars in a statement    */
  28. SpeechErr err;        /* error from speech routines        */
  29.  
  30. /*
  31.  * voice_init() - fire up the voice.
  32.  */
  33.  
  34. voice_init()
  35. {
  36.     if ((err = SpeechOn(noReader, &theSpeech))) {
  37.     theSpeech = (Handle) 0;
  38.     voicedfl();
  39.     return;
  40.     }
  41.     voicedfl();
  42. }
  43.  
  44. /*
  45.  * voice_stop() - close down the voice.
  46.  */
  47.  
  48. voice_stop()
  49. {
  50.     if (!theSpeech) return;
  51.  
  52.     SpeechOff(theSpeech);
  53. }
  54.  
  55. /*
  56.  * voiceset() - set the speech driver parameters
  57.  *  from the given items and values.
  58.  */
  59.  
  60. voiceset(gen, infl, pit, rat)
  61. Sex gen;            /* selected gender        */
  62. FOMode infl;            /* selected inflection        */
  63. int pit;            /* pitch (hz)            */
  64. int rat;            /* speaking rate (wpm)        */
  65. {
  66.     if (!theSpeech) return;
  67.  
  68.     SpeechSex(theSpeech, gen);
  69.     SpeechPitch(theSpeech, pit, infl);
  70.     SpeechRate(theSpeech, rat);
  71. }
  72.  
  73. /*
  74.  * criticize() - make one critical statement.
  75.  * "The <truthadj> <probnoun> is the <badadj> <badnoun> ."
  76.  */
  77.  
  78. criticize()
  79. {
  80.     char *tradjp;        /* points to the chosen truth adjective    */
  81.     char *prnounp;        /* "      "             problem noun    */
  82.     char *bdadjp;        /* "      "             bad adjective    */
  83.     char *bdnounp;        /* "      "             bad noun    */
  84.     static int first = 1;    /* "it's the first call"        */
  85.     static Handle statement;    /* phonetic sentence to speak        */
  86.  
  87.     if (!theSpeech) return;
  88.  
  89.     /*
  90.      * If this is the first call, init the structures
  91.      */
  92.  
  93.     if (first) {
  94.         first = 0;
  95.  
  96.     statement = NewHandle((Size) STATLEN);
  97.     for (tasize = 0; truthadj[tasize]; ++tasize)
  98.         ;
  99.     for (pnsize = 0; probnoun[pnsize]; ++pnsize)
  100.         ;
  101.     for (basize = 0; badadj[basize]; ++basize)
  102.         ;
  103.     for (bnsize = 0; badnoun[bnsize]; ++bnsize)
  104.         ;
  105.     }
  106.  
  107.     tradjp = truthadj[randint(tasize)];
  108.     prnounp = probnoun[randint(pnsize)];
  109.     bdadjp = badadj[randint(basize)];
  110.     bdnounp = badnoun[randint(bnsize)];
  111.  
  112.     HLock(statement);
  113.     strcpy((char *) *statement, vowel(tradjp) ? "DHIY " : "DHAH ");
  114.     strcat((char *) *statement, tradjp);
  115.     strcat((char *) *statement, " ");
  116.     strcat((char *) *statement, prnounp);
  117.     strcat((char *) *statement, " IHZ ");
  118.     strcat((char *) *statement, vowel(bdadjp) ? "DHIY " : "DHAH ");
  119.     strcat((char *) *statement, bdadjp);
  120.     strcat((char *) *statement, " ");
  121.     strcat((char *) *statement, bdnounp);
  122.     strcat((char *) *statement, ".#");
  123.     HUnlock(statement);
  124.  
  125.     if ((err = MacinTalk(theSpeech, statement))) {
  126.     return;
  127.     }
  128. }
  129.  
  130. /*
  131.  * vowel() - returns true if the given phonetically-encoded word
  132.  *  starts with a vowel.
  133.  */
  134.  
  135. int
  136. vowel(word)
  137. char *word;    /* a phonetic string suitable for MacinTalk */
  138. {
  139.     return(*word == 'A' ||
  140.            *word == 'E' ||
  141.            *word == 'I' ||
  142.            *word == 'O' ||
  143.            *word == 'U');
  144. }
  145.  
  146. /*
  147.  * randint() - return a random integer in the range 0 through range - 1.
  148.  */
  149. int
  150. randint(range)
  151. int range;
  152. {
  153.     static int firsttime = 1;/* True if the first time this has been called */
  154.     long secs;        /* value returned from GetDateTime()    */
  155.  
  156.     if (firsttime) {
  157.     firsttime = 0;
  158.     GetDateTime(&secs);
  159.     randSeed = secs;
  160.     }
  161.     return((unsigned) Random() % range);
  162. }
  163.